//
//  ZFScrollView.m
//  ZFScrollerView
//
//  Created by lanou on 16/6/22.
//  Copyright © 2016年 张凡. All rights reserved.
//

#import "ZFScrollView.h"
#define kWidthFrame self.frame.size.width
#define kHeightFrame self.frame.size.height
@interface ZFScrollView()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIImageView *leftImageView;

@property(nonatomic,strong)UIImageView *rightImageView;

@property(nonatomic,strong)NSArray *imageArray;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)UIPageControl *pageControl;

@property(nonatomic,strong)NSTimer *timer;
@end

@implementation ZFScrollView

- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        //创建滚动视图及其属性
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
        _scrollView.contentSize = CGSizeMake(kWidthFrame * 2, kHeightFrame);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        //创建小圆点
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kHeightFrame -50, 100, 50)];
        _pageControl.numberOfPages = self.imageArray.count;
    }
    return _pageControl;
}

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidthFrame, 0, kWidthFrame, kHeightFrame)];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
    return _rightImageView;
}
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.index = 1;
        //给数组赋值
        self.imageArray = imageArray;
        
        //设置视图
        [self createUI];
        
    }
    return self;
}
- (void)createUI{

    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.x == kWidthFrame) {
        
        scrollView.contentOffset = CGPointZero;
        
        self.pageControl.currentPage = self.index;
        
        
        self.index ++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
}
//定时器事件
- (void)timerAction{

    [self.scrollView setContentOffset:CGPointMake(kWidthFrame, 0) animated:YES];
}

@end
